#include <myhead.h>
typedef struct buf
{
	int apple;
	int orange;
}buff;

pthread_mutex_t mu1;
pthread_cond_t con1,con2;
buff a={0,0};

void*mytest1(void*arg);
void*mytest2(void*arg);
void*mytest3(void*arg);

int main(int argc, const char *argv[])
{
	pthread_mutex_init(&mu1,NULL);
	pthread_cond_init(&con1,NULL);
	pthread_cond_init(&con2,NULL);
	pthread_t t1,t2,t3;
	pthread_create(&t1,0,mytest1,0);
	pthread_create(&t2,0,mytest2,0);
	pthread_create(&t3,0,mytest3,0);
	while(1);

	return 0;
}
////////////////////////////////////////
void*mytest1(void*arg)
{
	while(1)
	{
		sleep(1);
		pthread_mutex_lock(&mu1);
		if(rand()%2==0)
		{
			a.apple+=2;
			printf("苹果+2:%d\n",a.apple);
		}
		else
		{
			a.orange+=3;
			printf("橘子+3:%d\n",a.orange);
		}
		if(a.apple>=5)
			pthread_cond_signal(&con1);
		if(a.orange>=7)
			pthread_cond_signal(&con2);
		pthread_mutex_unlock(&mu1);
	}
	return NULL;
}
/////////////////////////////////////////////////////////////
void*mytest2(void*arg)
{
	while(1)
	{
		sleep(2);
		pthread_mutex_lock(&mu1);
		pthread_cond_wait(&con1,&mu1);
		a.apple-=5;
		printf("-----------苹果-5:%d\n",a.apple);
		pthread_mutex_unlock(&mu1);
	}
}
void*mytest3(void*arg)
{
	while(1)
	{
		sleep(3);
		pthread_mutex_lock(&mu1);
		pthread_cond_wait(&con2,&mu1);
		a.orange-=7;
		printf("-----------橘子-7:%d\n",a.orange);
		pthread_mutex_unlock(&mu1);
	}
}
